<?php

namespace app\admin\controller;

use think\facade\Db;
use think\facade\Request;
use think\facade\View;
use app\admin\model\Ad as adModel;
use app\admin\model\Adsense as adsenseModel;

class Ad extends Common
{
    public function __construct()
    {
        parent::__construct();
    }


    /**
     * 广告列表
     * @author admin
     * @date 2019-08-08
     */
    public function index()
    {
        if (Request::isPost()) {
            $key = input('post.key');
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $list = Db::name('ad')->alias('a')
                ->join('ad_postion as', 'a.ad_postion_id = as.ad_postion_id', 'left')
                ->field('a.*,as.name as as_name')
                ->where('a.name|as.name', 'like', "%" . $key . "%")
                ->order('a.ad_id asc')
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return View::fetch();
    }


    public function doStatus()
    {
        try {
            $id = input('request.id');
            $status = input('request.status');
            if (!$id) {
                return ['code' => 3000, 'msg' => '操作失败!'];
            }
            $result = Db::name('ad')->where("ad_id", $id)->update(['status' => $status]);
            if ($result) {
                return ['code' => 0, 'msg' => '操作成功!'];
            }
            return ['code' => 3000, 'msg' => '操作失败!'];
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 添加广告
     * @author admin
     * @date 2019-08-08
     */
    public function add()
    {
        try {
            if (Request::isPost()) {
                //构建数组
                $data = Request::except(['file']);
                unset($data['id']);
                $data['create_time'] = time();
                $result = Db::name('ad')->insert($data);
                if ($result) {
                    return ['code' => 0, 'msg' => '添加成功！'];
                }
                return ['code' => 2000, 'msg' => '操作失败！'];
            } else {
                $adsense = Db::name('ad_postion')->select()->toArray();
                $this->assign('adsense', $adsense);
                return View::fetch('edit');
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 编缉广告
     * @author admin
     * @date 2019-08-08
     */

    public function edit()
    {
        try {
            if (Request::isPost()) {
                $data = Request::except(['file']);
                $id = $data['id'];
                unset($data['id']);
                Db::name('ad')->where('ad_id',$id)->update($data);
                return ['code' => 0, 'msg' => '修改成功！'];
            } else {
                $id = input('get.id');
                $adsense = Db::name('ad_postion')->select()->toArray();
                $info = Db::name('ad')->where('ad_id',$id)->find();
                $this->assign('info', $info);
                $this->assign('adsense', $adsense);
                return View::fetch();
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


    public function doDelete()
    {
        $id = input('id', 0, "intval");
        if (!$id) {
            return ['code' => 2000, 'msg' => '非法请求'];
        }
        try {
            Db::name('ad')->where('ad_id', '=', $id)->delete();//删除该广告位所有广告
            return ['code' => 0, 'msg' => '删除成功！'];
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => '操作失败！'];
        }
    }
}